package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.QiniuUtils;
import com.qiniu.storage.model.DefaultPutRet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    @Reference
    SetMealService setMealService;

    @Autowired
    JedisPool jedisPool;

    @RequestMapping("/upload")
    @PreAuthorize("hasAnyAuthority('SETMEAL_ADD','SETMEAL_EDIT')")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) throws IOException {
        try {
            DefaultPutRet upload = QiniuUtils.upload(imgFile.getBytes());
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,upload.key);
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,upload.key);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    public Result add(@RequestBody Setmeal setMeal,Integer[] checkgroupIds){
        try {
            setMealService.add(setMeal,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return setMealService.findPage(queryPageBean);
    }

    @RequestMapping("/del/{id}")
    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    public Result del(@PathVariable("id")int id){
        try {
            setMealService.del(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除此套餐失败" );
        }
        return new Result(true,"删除此套餐成功" );
    }

    @RequestMapping("find/{id}")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    public Result findById(@PathVariable("id")int id){
        Setmeal setmeal= null;
        try {
            setmeal = setMealService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            new Result(false,MessageConstant.QUERY_SETMEAL_FAIL );
        }
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal );
    }

    @RequestMapping("/update")
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    public Result update(@RequestBody Setmeal setMeal,Integer[] checkgroupIds){
        try {
            setMealService.update(setMeal,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

}
